package com.shark.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @auther shark
 * @create 2024-08-09 23:13
 */
@Component
@Slf4j
public class MyMessageListener4DeadLetter
{
    public static final String EXCHANGE_NORMAL = "exchange.normal.video";
    public static final String EXCHANGE_DEAD_LETTER = "exchange.dead.letter.video";

    public static final String ROUTING_KEY_NORMAL = "routing.key.normal.video";
    public static final String ROUTING_KEY_DEAD_LETTER = "routing.key.dead.letter.video";

    public static final String QUEUE_NORMAL = "queue.normal.video";
    public static final String QUEUE_DEAD_LETTER = "queue.dead.letter.video";

    @RabbitListener(queues = {QUEUE_NORMAL})
    public void processMessageNormal(Message message, Channel channel) throws IOException
    {
        // 监听正常队列，但是拒绝消息
//        log.info("[normal]消息接收到，但我拒绝签收，情况1。");
//        channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);

        // 监听正常队列
        log.info("[normal]消息接收到，正在逐条消费，情况2");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);


//        log.info("[normal]消息接收到，正在逐条消费，情况3");

    }

    @RabbitListener(queues = {QUEUE_DEAD_LETTER})
    public void processMessageDead(String dataString, Message message, Channel channel) throws IOException {
        // 监听死信队列
        log.info("★[dead letter]dataString = " + dataString);
        log.info("★[dead letter]我是死信监听方法，我接收到了死信消息");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
